package cn.liccshop.sys.base;

import cn.liccshop.sys.constants.Constants;
import cn.liccshop.sys.entity.Result;
import cn.liccshop.sys.exception.MySystemException;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Collection;
import java.util.List;

/**
 * @program: LiCCShop
 * @description: 常用接口实现
 * @author: LiCC
 * @data 2019-03-15-22:52
 */
public class MyActionController<T extends BaseBean> extends BaseController<T> {

    /**
     * 添加数据
     */
    protected static final String L_DATA_INSERT = "/data/insert";
    /**
     * 删除数据
     */
    protected static final String L_DATA_DELETE = "/data/delete";
    /**
     * 删除数据通过IDs
     */
    protected static final String L_DATA_DELETE_BY_IDS = "/data/deleteByIds";
    /**
     * 更新单条数据通过ID
     */
    protected static final String L_DATA_UPDATE_BY_ID = "/data/updateById";
    /**
     * 批量更新数据
     */
    protected static final String L_DATA_UPDATES = "/data/updates";
    /**
     * 查询单条数据
     */
    protected static final String L_DATA_INFO = "/data/info";
    /**
     * 查询多条数据
     */
    protected static final String L_DATA_LIST = "/data/list";
    /**
     * 查询分页查询
     */
    protected static final String L_DATA_LIST_BY_PAGE = "/data/listByPage";

    @RequestMapping(L_DATA_INSERT)
    public Result dataInsert(@RequestBody T entity) throws MySystemException {
        IService<T> service = this.theService();
        Boolean isSuccess = service.save(entity);
        return isSuccess == true ? new Result().setCode(Constants.OK).setData(entity).setMessage("保存成功")
                : new Result().setCode(Constants.FAIL).setData(entity).setMessage("保存失败");
    }

    @RequestMapping(L_DATA_DELETE)
    public Result dataDelete(@RequestBody T entity) throws MySystemException {
        IService service = this.theService();
        Wrapper<T> wrapper = new QueryWrapper<>(entity);
        Boolean isSuccess = service.remove(wrapper);
        return isSuccess == true ? new Result().setCode(Constants.OK).setData(entity).setMessage("删除成功")
                : new Result().setCode(Constants.FAIL).setData(entity).setMessage("删除失败");
    }

    @RequestMapping(L_DATA_DELETE_BY_IDS)
    public Result dataDeleteByIds(@RequestBody Collection<String> ids) throws MySystemException {
        IService service = this.theService();
        Boolean isSuccess = service.removeByIds(ids);
        return isSuccess == true ? new Result().setCode(Constants.OK).setData(ids).setMessage("删除成功")
                : new Result().setCode(Constants.FAIL).setData(ids).setMessage("删除失败");
    }

    @RequestMapping(L_DATA_UPDATE_BY_ID)
    public Result dataUpdateById(@RequestBody T entity) throws MySystemException {
        IService<T> service = this.theService();
        Boolean isSuccess = service.updateById(entity);
        return isSuccess == true ? new Result().setCode(Constants.OK).setData(entity).setMessage("更新成功")
                : new Result().setCode(Constants.FAIL).setData(entity).setMessage("更新失败");
    }

    @RequestMapping(L_DATA_UPDATES)
    public Result dataUpdates(@RequestBody Collection<T> entities) throws MySystemException {
        IService<T> service = this.theService();
        Boolean data = service.updateBatchById(entities);
        return data == true ? new Result().setCode(Constants.OK).setMessage("更新成功")
                : new Result().setCode(Constants.FAIL).setMessage("更新失败");
    }

    @RequestMapping(L_DATA_INFO)
    public Result dataInfo(@RequestBody T entity) throws MySystemException {
        IService<T> service = this.theService();
        T data = service.getOne(new QueryWrapper<>(entity));
        return new Result().setCode(Constants.OK).setData(data);
    }

    @RequestMapping(L_DATA_LIST)
    public Result dataList(@RequestBody T entity) throws MySystemException {
        IService<T> service = this.theService();
        List<T> data = service.list(new QueryWrapper<>(entity));
        return new Result().setCode(Constants.OK).setData(data);
    }

    @RequestMapping(L_DATA_LIST_BY_PAGE)
    public Result dataListByPage(@RequestParam("page") Integer page, @RequestParam("size") Integer size, @RequestBody T entity) throws MySystemException {
        IPage<T> iPage = new Page<>(page, size);
        Wrapper<T> wrapper = new QueryWrapper<>(entity);
        this.theService().page(iPage, wrapper);
        return new Result().setCode(Constants.OK).setData(iPage);
    }
}
